package com.family.gmall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.family.gmall.model.enums.PaymentStatus;
import com.family.gmall.model.enums.PaymentType;
import com.family.gmall.model.order.OrderInfo;
import com.family.gmall.model.order.PaymentInfo;
import com.family.gmall.order.client.OrderFeignClient;
import com.family.gmall.payment.config.AlipayConfig;
import com.family.gmall.payment.service.AlipayService;
import com.family.gmall.payment.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: family
 * @Date: 2023/8/6 20:40
 * @Description:
 */
@Service
public class AliPayServiceImpl implements AlipayService {


    @Resource
    private OrderFeignClient orderFeignClient;

    @Resource
    private PaymentService paymentService;

    @Resource
    private AlipayClient alipayClient;

    @Override
    public String createAliPay(Long orderId) {
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        if ("APID".equals(orderInfo.getOrderStatus()) || "CLOSED".equals(orderInfo.getOrderStatus())) {
            return "该订单已经完成或已经关闭";
        }
        paymentService.savePaymentInfo(orderInfo, PaymentType.ALIPAY.name());
        String form = "";
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_payment_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_payment_url);
        Map<String, Object> map = new HashMap<>();
        map.put("out_trade_no", orderInfo.getOutTradeNo());
        map.put("total_amount", orderInfo.getTotalAmount());
        map.put("subject", orderInfo.getOutTradeNo());
        map.put("body", orderInfo.getTradeBody());
        map.put("product_code", "FAST_INSTANT_TRADE_PAY");
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String date = sdf.format(orderInfo.getExpireTime());
        // map.put("time_expire", date);
        alipayRequest.setBizContent(JSON.toJSONString(map));
        try {
            //调用SDK生成表单
            form = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }

    @Override
    public boolean refund(Long orderId) {

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        Map<String, Object> map = new HashMap<>();
        map.put("out_trade_no", orderInfo.getOutTradeNo());
        map.put("refund_amount", orderInfo.getTotalAmount());
        map.put("refund_reason", "颜色浅了点");

        request.setBizContent(JSON.toJSONString(map));
        AlipayTradePagePayResponse response = null;

        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
            paymentService.updatePaymentInfo(orderInfo.getOutTradeNo(), orderInfo.getPaymentWay(), paymentInfo);
            return true;
        }
        return false;
    }
}
